package com.southminority.ethnic.controller.research;

import com.southminority.ethnic.common.result.PageResult;
import com.southminority.ethnic.common.result.R;
import com.southminority.ethnic.service.research.PaperService;
import com.southminority.ethnic.controller.research.vo.PaperVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 学术论文前端展示控制器
 * 
 * @author SouthMinority
 */
@Slf4j
@Tag(name = "16 - 学术论文展示API", description = "【前台】科研成果-学术论文查询与展示相关接口")
@RestController
@RequestMapping("/api/research/paper")
@CrossOrigin
public class PaperController {

    private final PaperService paperService;

    public PaperController(PaperService paperService) {
        this.paperService = paperService;
    }

    @GetMapping("/page")
    @Operation(summary = "分页和条件查询已发布的学术论文列表", description = "用于前端论文列表页面，支持按关键词、论文类型和发表年份筛选。")
    @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "查询成功")})
    public R<PageResult<PaperVO>> getPublishedPaperPage(
            @Parameter(description = "当前页码", example = "1") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页显示数量", example = "10") @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "关键词（标题、作者、关键字）", example = "语音识别") @RequestParam(required = false) String keyword,
            @Parameter(description = "论文类型", example = "期刊论文") @RequestParam(required = false) String paperType,
            @Parameter(description = "发表年份", example = "2023") @RequestParam(required = false) Integer year) {
        return R.ok(paperService.getPublishedPaperPage(page, size, keyword, paperType, year));
    }

    @GetMapping("/detail/{id}")
    @Operation(summary = "获取单个学术论文的详细信息", description = "根据论文ID查询，返回其完整的公开信息。")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "404", description = "论文不存在或未发布")
    })
    public R<PaperVO> getPaperDetail(@Parameter(description = "论文的唯一ID", example = "1", required = true) @PathVariable Long id) {
        return R.ok(paperService.getPaperDetail(id));
    }

    @PostMapping("/download/{id}")
    @Operation(summary = "记录论文下载次数", description = "调用此接口，对应论文的下载次数字段将+1。")
    @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "操作成功")})
    public R<Void> increaseDownloadCount(@Parameter(description = "论文的唯一ID", example = "1", required = true) @PathVariable Long id) {
        paperService.increaseDownloadCount(id);
        return R.ok();
    }
    
    @GetMapping("/years")
    @Operation(summary = "获取所有论文发表年份列表", description = "用于前端筛选菜单，返回一个包含所有不重复年份的列表。")
    @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "获取成功")})
    public R<List<Integer>> getPaperYears() {
        return R.ok(paperService.getPaperYears());
    }

    @GetMapping("/types")
    @Operation(summary = "获取所有论文类型列表", description = "用于前端筛选菜单，返回一个包含所有不重复论文类型的字符串列表。")
    @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "获取成功")})
    public R<List<String>> getPaperTypes() {
        return R.ok(paperService.getPaperTypes());
    }

    @GetMapping("/test")
    @Operation(summary = "测试接口连通性")
    public R<String> test() {
        return R.ok("学术论文前端服务正常");
    }
} 
